﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Utility;


public partial class Admin_VerifyCircuit_VerifyCircuit_Add : System.Web.UI.Page
{
    VerifyCircuitManager dllVerifyCircuitManager = new VerifyCircuitManager();
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int _levelCount = Convert.ToInt32(this.drpLevel.SelectedValue);
            bindGridView(_levelCount);
            bindDropDownList(this.drpDepartmetnName);

        }
    }

    /// <summary>
    /// 根据选择的级数绑定GridView
    /// </summary>
    /// <param name="_levelCount">选择的级数</param>
    /// <author>grokyao</author>
    /// <date>2009-1-9</date>
    void bindGridView(int _levelCount)
    {
        DataTable table = new DataTable();
        DataColumn dc1 = new DataColumn("id");
        DataColumn dc2 = new DataColumn("审核人");
        table.Columns.Add(dc1);
        table.Columns.Add(dc2);
        for (int i = 0; i < _levelCount; i++)
        {
            DataRow dr = table.NewRow();
            table.Rows.Add(dr);
        }
        this.GridView1.DataSource = table;
        this.GridView1.DataBind();
        //表示级数变量
        int count = 0;
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            count = i + 1;
            this.GridView1.Rows[i].Cells[0].Text = "第" + count + "级";
        }
    }


    /// <summary>
    /// DropDownList索引改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        int _levelCount = Convert.ToInt32(this.drpLevel.SelectedValue);
        bindGridView(_levelCount);
    }


    /// <summary>
    /// 提交按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-8</date>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //判断是否选择级数
        if (this.drpLevel.SelectedValue == "0")
        {
            Utility.HttpHelp.alert("请选择流程级数", Page);
            return;
        }
        int _count = 0;
        int VerifyCircuitId = dllVerifyCircuitManager.maxVerifyCircuitId() + 1;

        DataTable _userTable = new DataTable();
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            HtmlInputText tb = (HtmlInputText)this.GridView1.Rows[i].Cells[1].Controls[1];
            object[] _user = new object[1];
            _user[0] = tb.Value.Trim();
            _userTable = dllUserInfoManager.get(UserInfoManager.field.RealName.ToString(), _user);
            if (_userTable.Rows.Count == 0)
            {
                continue;
            }
            object[] _value = new object[5];
            _value[0] = this.txtVerifyName.Text.Trim();
            _value[1] = VerifyCircuitId;
            _value[2] = _userTable.Rows[0]["UserName"].ToString();
            _value[3] = tb.Value.Trim();
            _value[4] = this.drpDepartmetnName.SelectedItem.Text;
            int result = dllVerifyCircuitManager.add(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加审核流程未成功，请重试！");
            }
            else
            {
                _count++;
            }

        }
        //判断是否全部完成！！
        if (_count == this.GridView1.Rows.Count)
        {
            Response.Redirect("VerifyCircuitManager.aspx");
        }
        else
        {
            //回滚数据库
            Utility.Error.RedirectErrorPage("添加审核流程未成功，请重试！");
        }
    }



    /// <summary>
    /// 绑定部门bindDropDownList
    /// </summary>
    void bindDropDownList(DropDownList _dropDownList)
    {
        if (_dropDownList.Equals(this.drpDepartmetnName))
        {
            _dropDownList.DataSource = dllDepartment.getNoTree();
            _dropDownList.DataTextField = "DepartmentName";
            _dropDownList.DataValueField = "DepartmentId";
            _dropDownList.DataBind();
            if (_dropDownList.Items.Count == 0)
            {
                ListItem li = new ListItem("无部门");
                _dropDownList.Items.Add(li);
            }
        }
    }


    /// <summary>
    /// GridView行创建事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }
    protected void btnback_Click(object sender, EventArgs e)
    {
        Response.Redirect("VerifyCircuitManager.aspx");
    }
}
